$().ready(function () {
    $('#role').hide();
    validateRule();
    firstList();
    $('.summernote').summernote({
        height: '300px',
        lang: 'zh-CN',
        toolbar: [
            ['style', ['bold', 'italic', 'underline', 'clear']],
            ['font', ['strikethrough', 'superscript', 'subscript']],
            ['fontsize', ['fontsize']],
            ['fontname', ['fontname']],
            ['color', ['color']],
            ['para', ['ul', 'ol', 'paragraph']],
            ['height', ['height']],
            ['table', ['table']],
            ['insert', ['link', 'picture']],
            ['view', ['fullscreen', 'codeview', 'help']]
        ],
        callbacks: {
            onImageUpload: function (files, editor, $editable) {
                sendFile(files);
            }
        }
    });
    //判断是否为资料文库，是则隐藏富文本
    // $("#secondColumnDiv").on("change", "#secondColumn", function () {
    //     if ($("#secondColumn").val() == 'tra_information_library' || $("#secondColumn").val() == 'cer_information_library'
    //         || $("#secondColumn").val() == 'cer_exam_notes' || $("#secondColumn").val() == 'cer_exam_notes' || $("#secondColumn").val() == 'cer_registration_notice'
    //         || $("#secondColumn").val() == 'cer_expert_application_template' || $("#secondColumn").val() == 'cer_site_application_template' || $("#secondColumn").val() == 'evaluation_staff_apply_temp'
    //         || $("#secondColumn").val() == 'organization_member_apply_temp' || $("#secondColumn").val() == 'lecturer_apply_temp'
    //         || $("#secondColumn").val() == 'supervisor_apply_temp' || $("#secondColumn").val() == 'check_point_apply_temp'
    //         || $("#secondColumn").val() == 'evaluation_point_apply_temp') {
    //         $("#isContents").hide();
    //     } else {
    //         $("#isContents").show();
    //     }
    //
    //     if ($("#secondColumn").val() == 'skill_level' || $("#secondColumn").val() == 'teaching'
    //         || $("#secondColumn").val() == 'score_criteria' || $("#secondColumn").val() == 'evaluation_criteria' || $("#secondColumn").val() == 'course_criteria'
    //         || $("#secondColumn").val() == 'operation_manual' || $("#secondColumn").val() == 'other_data' ) {
    //         $("#role").show();
    //     } else {
    //         $("#role").hide();
    //     }
    //
    // })

    //分页下拉框
    $("#firstColumnDiv").on("change", "#firstColumn", function () {
        $.ajax({
            url: '/jzweb/consultations/dicLists',
            data: {type: $("#firstColumn").val()},
            success: function (r) {
                var list = '<select id="secondColumn" name="secondColumn" class="form-control">';
                if (r.data.length == 0) {
                    list += '<option value="">暂无数据</option>';
                } else {
                    list += '<option value="">请选择</option>';
                    for (var p in r.data) {
                        list += '<option value="' + r.data[p].type + '">' + r.data[p].value + '</option>';
                    }
                }
                list += '</select>';
                $("#secondColumnDiv").html(list);
            }
        })
    })

});

function getCheckedRoles() {
	var adIds = "";
	$("input:checkbox[name=role]:checked").each(function(i) {
		if (0 == i) {
			adIds = $(this).val();
		} else {
			adIds += ("," + $(this).val());
		}
	});
	return adIds;
}

function firstList() {
    $.ajax({
        cache: true,
        type: "GET",
        url: "/jzweb/consultations/dicLists",
        data: {type: ""},
        success: function (r) {
            var list = '<select id="firstColumn" name="firstColumn" class="form-control">';
            if (r.data.length == 0) {
                list += '<option value="">暂无数据</option>';
            } else {
                list += '<option value="">请选择</option>';
                for (var p in r.data) {
                    list += '<option value="' + r.data[p].type + '">' + r.data[p].value + '</option>';
                }
            }
            list += '</select>';
            $("#firstColumnDiv").html(list);
        }
    });
}

$.validator.setDefaults({
    submitHandler: function () {
        $("#contents").val($('.summernote').summernote('code'));
        //console.log("$(contents).val()::::::::::::", $("#contents").val());
        save();
    }
});

function save() {
    $("#roleIds").val(getCheckedRoles());
    $.ajax({
        cache: true,
        type: "POST",
        url: "/jzweb/consultations/save",
        data: $('#signupForm').serialize(),
        async: false,
        error: function (request) {
            parent.layer.alert("Connection error");
        },
        success: function (data) {
            if (data.code == 0) {
                parent.layer.msg("操作成功");
                parent.reLoad();
                var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                parent.layer.close(index);

            } else {
                parent.layer.alert(data.msg)
            }

        }
    });

}

function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#signupForm").validate({
        rules: {
            firstColumn: {
                required: true
            },
            secondColumn: {
                required: true
            },
            title: {
                required: true
            },
            // contents : {
            //     required : true
            // }
        },
        messages: {
            firstColumn: {
                required: icon + "请输入一级栏目"
            },
            secondColumn: {
                required: icon + "请输入二级栏目"
            },
            title: {
                required: icon + "请输入标题"
            },
            // contents : {
            //     required : icon + "请输入内容"
            // }
        }
    })
}


layui.use('upload', function () {
    var upload = layui.upload;
    upload.render({
        elem: '#btnUpload', //绑定元素
        url: '/common/sysFile/upload', //上传接口
        accept: 'file',
        done: function (r) {
            $("#files").val(r.data);
            $("#test").html(subFileName(r.data))
        },
        error: function (r) {
            layer.msg(r.msg);
        }
    });
    upload.render({
        elem: '#btnUpload1',
        url: '/common/sysFile/upload',
        accept: 'images', //只允许上传图片
        acceptMime: 'image/*', //只筛选图片
        done: function (r) {
            $("#thumbnail").val(r.data);
            $("#img").attr("src", r.data)
        },
        error: function (r) {
            layer.msg(r.msg);
        }
    });
});